package com.system.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.system.business.domain.DailyTrain;
import com.system.business.domain.DailyTrainStation;
import com.system.business.domain.TrainStation;
import com.system.business.req.DailyTrainStationQueryReq;
import com.system.business.req.DailyTrainStationSaveReq;
import com.system.business.resp.DailyTrainQueryResp;
import com.system.business.resp.DailyTrainStationQueryResp;
import com.system.business.service.DailyTrainStationService;
import com.system.business.mapper.DailyTrainStationMapper;
import com.system.business.service.TrainStationService;
import com.system.common.resp.PageResp;
import com.system.common.utils.SnowUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
* @author Admin
* @description 针对表【daily_train_station(每日车站)】的数据库操作Service实现
* @createDate 2024-03-21 15:02:15
*/
@Service
public class DailyTrainStationServiceImpl extends ServiceImpl<DailyTrainStationMapper, DailyTrainStation>
    implements DailyTrainStationService{
    @Autowired
    private DailyTrainStationMapper dailyTrainStationMapper;
    @Autowired
    private TrainStationService trainStationService;

    @Override
    public PageResp<DailyTrainStationQueryResp> queryList(DailyTrainStationQueryReq req) {
        Page<DailyTrainStation> pageCondition = new Page<>(req.getPage(), req.getSize());
        QueryWrapper<DailyTrainStation> passengerQueryWrapper = new QueryWrapper<>();
        Page<DailyTrainStation> pageData = dailyTrainStationMapper.selectPage(pageCondition,passengerQueryWrapper);
        List<DailyTrainStationQueryResp> list = BeanUtil.copyToList(pageData.getRecords(), DailyTrainStationQueryResp.class);

        PageResp<DailyTrainStationQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageData.getTotal());
        pageResp.setList(list);
        return pageResp;
    }

    @Override
    public void save(DailyTrainStationSaveReq req) {
        DateTime now = DateTime.now();

        DailyTrainStation dailyTrainStation = BeanUtil.copyProperties(req, DailyTrainStation.class);
        if (ObjectUtil.isNull(dailyTrainStation.getId())) {
            dailyTrainStation.setId(SnowUtil.getSnowflakeNextId());
            dailyTrainStation.setCreateTime(now);
            dailyTrainStation.setUpdateTime(now);
            dailyTrainStationMapper.insert(dailyTrainStation);
        } else {
            dailyTrainStation.setUpdateTime(now);
            dailyTrainStationMapper.updateById(dailyTrainStation);
        }
    }
    @Transactional
    @Override
    public void genDaily(Date date, String trainCode) {

        // 删除某日某车次的车站信息
        QueryWrapper<DailyTrainStation> dailyTrainStationQueryWrapper = new QueryWrapper<>();
        dailyTrainStationQueryWrapper.eq("train_code",trainCode).eq("date",date);
        dailyTrainStationMapper.delete(dailyTrainStationQueryWrapper);

        QueryWrapper<TrainStation> trainStationQueryWrapper = new QueryWrapper<>();
        // 查出某车次的所有的车站信息
        List<TrainStation> stationList = trainStationService.list(trainStationQueryWrapper);
        if (CollUtil.isEmpty(stationList)) {
            return;
        }

        for (TrainStation trainStation : stationList) {
            DateTime now = DateTime.now();
            DailyTrainStation dailyTrainStation = BeanUtil.copyProperties(trainStation, DailyTrainStation.class);
            dailyTrainStation.setId(SnowUtil.getSnowflakeNextId());
            dailyTrainStation.setCreateTime(now);
            dailyTrainStation.setUpdateTime(now);
            dailyTrainStation.setDate(date);
            dailyTrainStationMapper.insert(dailyTrainStation);
        }
    }
}




